package com.github.xaidanwang.auth.server.handler;

import com.alibaba.fastjson.JSON;
import com.github.xaidanwang.auth.server.config.JwtConfigPo;
import com.github.xaidanwang.auth.server.util.RedisUtil;
import com.github.xaidanwang.response.CommonResult;
import com.github.xaidanwang.response.ConstantEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author wang yi fei
 * @date 2019/10/8 16:15
 */
@Component
@Slf4j
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {
	@Autowired
	JwtConfigPo jwtConfigPo;
	@Autowired
	RedisUtil redisUtil;
	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
		response.setHeader("Content-Type", "application/json;charset=utf-8");
		CommonResult commonResult = CommonResult.buildWithDatAndMessage(ConstantEnum.GLOBAL_SUCCESS,null,"登出成功");
		sendResponse(response, JSON.toJSONString(commonResult));
	}

	private void sendResponse(HttpServletResponse response,String body){
		try {
			response.getWriter().write(body,0,body.length());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


}
